package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.qy.dao.DriveLineDao;
import java.util.List;
import java.util.Map;
import java.sql.Types;
import com.qy.db.DbParameter;
import java.util.HashMap;
import java.util.ArrayList;
import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.DriveLine;
import com.qy.db.BaseDao;

/**
* u_drive_line daoImpl实现类的生成
* @author zhangwenlong
* @date Sat Feb 03 16:27:37 GMT+08:00 2018
*/ 
public class DriveLineDaoImpl extends BaseDao  implements DriveLineDao {

	@Override
	public boolean createDriveLine(DriveLine driveLine) {
		boolean result = false;
		int id = -1;
		if (driveLine.beginAdd()) {
			String sql = "insert into u_drive_line (id,driver_order_id,drive_line,create_date) values (?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, driveLine.getId()));
			param.put(2, new DbParameter(Types.INTEGER, driveLine.getDriverOrderId()));
			param.put(3, new DbParameter(Types.VARCHAR, driveLine.getDriveLine()));
			param.put(4, new DbParameter(Types.TIMESTAMP, driveLine.getCreateDate()));
			id = execLastId(sql, param);
			result = id > -1;
			driveLine.commitAdd(result);
			driveLine.setId(id);
		}
		return result;
	}

	@Override
	public List<DriveLine> getDriveLineList() {
		String sql = "select * from u_drive_line;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<DriveLine> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<DriveLine>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public DriveLine getDriveLineById(int id) {
		String sql = "select * from u_drive_line where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		DriveLine driveLine = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					driveLine = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return driveLine;
	}

	@Override
	public boolean updateDriveLine(DriveLine driveLine) {
		boolean result = false;
		if (driveLine.beginUpdate()) {
			String sql = "update u_drive_line set driver_order_id=?,drive_line=?,create_date=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, driveLine.getDriverOrderId()));
			param.put(2, new DbParameter(Types.VARCHAR, driveLine.getDriveLine()));
			param.put(3, new DbParameter(Types.TIMESTAMP, driveLine.getCreateDate()));
			param.put(4, new DbParameter(Types.INTEGER, driveLine.getId()));
			result = execNoneQuery(sql, param) > -1;
			driveLine.commitUpdate(result);
		}
		return result;
	}

	public DriveLine resultTo(ResultSet rs) throws SQLException {
		DriveLine driveLine = new DriveLine();
		driveLine.setId(rs.getInt("id"));
		driveLine.setDriverOrderId(rs.getInt("driver_order_id"));
		driveLine.setDriveLine(rs.getString("drive_line"));
		driveLine.setCreateDate(rs.getTimestamp("create_date"));
		driveLine.setOp(Option.None);
		return driveLine;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

	@Override
	public DriveLine getDriveLineByDoId(int driverOrderId) {
	   	String sql = "select * from u_drive_line where driver_order_id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, driverOrderId));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		DriveLine driveLine = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					driveLine = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return driveLine;
	}

}

